package com.example.togethermove.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.togethermove.config.Config;


public class SPUtilsone {
    private static SharedPreferences sp;

    public static SharedPreferences getInstance(Context context) {
        synchronized (SPUtilsone.class) {
            if (sp == null) {
                sp = context.getSharedPreferences(Encry.enCry(Config.SP_NAME), Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);
            }
        }
        return sp;
    }

    public static void clearAllData() {
        checkSp();
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.apply();
    }

    public static void putStringValue(String key, String value) {
        checkSp();
        key = Encry.enCry(key);
        value = Encry.enCry(value);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static String getStringValue(String key, String defValue) {
        checkSp();
        key = Encry.enCry(key);
        String data = sp.getString(key, defValue);
        if (data != null && !data.equals(defValue)) {
            data = Encry.deCry(data);
        }
        return data;
    }

    public static void remoceKey(String key) {
        checkSp();
        key = Encry.enCry(key);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.apply();
    }

    public static void putBooleanValue(String key, boolean value) {
        putStringValue(key, String.valueOf(value));
    }

    public static boolean getBooleanValue(String key, boolean defValue) {
        boolean value = defValue;
        try {
            value = Boolean.valueOf(getStringValue(key, String.valueOf(defValue)));
        }catch (Exception e){

        }
        return value;
    }
    public static void putIntValue(String key, int value) {
        putStringValue(key, String.valueOf(value));
    }
    public static int getIntValue(String key, int defValue) {
        int value = defValue;
        try {
            value = Integer.valueOf(getStringValue(key, String.valueOf(defValue)));
        }catch (Exception e){

        }
        return value;
    }

    public static void putFloatValue(String key, float value) {
        putStringValue(key, String.valueOf(value));
    }
    public static float getFloatValue(String key, float defValue) {
        float value = defValue;
        try {
            value = Float.valueOf(getStringValue(key, String.valueOf(defValue)));
        }catch (Exception e){

        }
        return value;
    }



    private static void checkSp() {
        if (sp == null) {
//                sp = getInstance(MyApp.getAppContext());
        }
    }


}
